export default class extends think.BaseController {
  constructor(ctx) {
    super(ctx);
    this.MenuService = think.service('sys/MenuService');
  }
  indexAction() {}
  /**
     * 新增/修改菜单
     *
     * @returns
     */
  async doAddOrUpdateAction() {
    const aj = think.ajaxJson();
    // console.log(this.post());
    const PostData = this.post('id,menu_level,menu_name,menu_order,menu_url,comp_path,parent_id,menu_type,menu_i' +
        'con,menu_source,is_show,is_button,is_crumb,href,permission,parent,parent_ids');
    const res = await this
      .MenuService
      .doAddOrUpdate(PostData);
    if (!res.error) {
      aj.success = res.success;
      aj.msg = res.msg;
    } else {
      aj.success = false;
      aj.msg = `查询失败:${res.error}`;
    }
    return this.json(aj);
  }
  /**
     * 查询菜单信息  （分页）
     *
     * @returns
     */
  async doQueryMenuListAction() {
    const aj = think.ajaxJson();
    const GetData = this.get('page,pageSize,menu_name');
    const res = await this
      .MenuService
      .getSysMenuList(GetData);
    if (!res.error) {
      aj.success = res.success;
      aj.msg = res.msg;
      aj.result = res.result;
    } else {
      aj.success = false;
      aj.msg = `查询失败:${res.error}`;
    }
    return this.json(aj);
  }

  /**
     * 删除菜单
     *
     * @returns
     */
  async delSysMenuInfoAction() {
    const aj = think.ajaxJson();
    const postData = {
      id: this.post('id')
    };
    const res = await this
      .MenuService
      .delSysMenuInfo(postData);
    if (!res.error) {
      aj.success = res.success;
      aj.msg = res.msg;
    } else {
      aj.success = false;
      aj.msg = `查询失败:${res.error}`;
    }
    return this.json(aj);
  }

  /**
     * 查询菜单信息 全部
     *
     * @returns
     */
  async doQueryMenuAllAction() {
    const aj = think.ajaxJson();
    const res = await this
      .MenuService
      .getAllMenu();
    if (!res.error) {
      aj.success = res.success;
      aj.msg = res.msg;
      aj.result = res.result;
    } else {
      aj.success = false;
      aj.msg = `查询失败:${res.error}`;
    }
    return this.json(aj);
  }

  /**
     * 查询父级菜单
     *
     * @returns
     */
  async doQueryMenuLevelAction() {
    const aj = think.ajaxJson();
    const res = await this
      .MenuService
      .getsysMenuLevel(this.get());
    if (!res.error) {
      aj.success = res.success;
      aj.msg = res.msg;
      aj.result = res.result;
    } else {
      aj.success = false;
      aj.msg = `查询失败:${res.error}`;
    }
    return this.json(aj);
  }
  async getMenuListByAction() { // 条件查询菜单信息
    const getData = this.get();
    const res = await this
      .MenuService
      .getSysMenuList(getData);
    return this.json(res);
  }

  async getMenuListByidAction() { // 条件查询菜单信息
    const aj = think.ajaxJson();
    const getData = {
      id: this.get('id')
    };
    const res = await this
      .MenuService
      .getSysMenuListById(getData);

    if (res && res.error) {
      aj.success = false;
      aj.msg = '获取失败';
    } else {
      aj.result = res;
    }
    return this.json(aj);
  }
  async getMenuListByIdsAction() { // 条件查询菜单信息
    const aj = think.ajaxJson();
    const getData = this.get('ids,id,page,pageSize');
    const res = await this
      .MenuService
      .getSysMenuListByIds(getData);

    if (res && res.error) {
      aj.success = false;
      aj.msg = '获取失败';
    } else {
      aj.result = res;
    }
    return this.json(aj);
  }
}
